package com.nhjf.mobile.controller.finetTalk;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aliyuncs.utils.StringUtils;
import com.nhjf.common.page.Pagination;
import com.nhjf.common.util.StringUtil;
import com.nhjf.mobile.common.util.SysProperties;
import com.nhjf.model.finetTalk.FinetTalk;
import com.nhjf.model.teachers.CoursesComment;
import com.nhjf.pojo.dto.finetTalk.FinetTalkDto;
import com.nhjf.pojo.qo.finetTalk.FinetTalkCommentQo;
import com.nhjf.pojo.qo.finetTalk.FinetTalkQo;
import com.nhjf.pojo.qo.teachers.CoursesCommentQo;
import com.nhjf.service.finetTalk.FinetTalkCommentService;
import com.nhjf.service.finetTalk.FinetTalkService;
import com.nhjf.service.system.AdminSessionService;
import com.nhjf.service.system.UserService;
import com.nhjf.service.teachers.CoursesCommentService;
import com.nhjf.service.teachers.TeachersService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/finetTalk")
public class FinetTalkCountroller {

	@Autowired
	private AdminSessionService adminSessionService;
	@Autowired
	private UserService userService;
	@Autowired
	private FinetTalkService finettalkService;
	@Autowired
	private TeachersService teachersService;
	@Autowired
	private FinetTalkCommentService finetTalkCommentService;

	//精讲：精讲列表
	@ResponseBody
	@RequestMapping("/speak_list")
	public String speak(@RequestParam(value = "page", defaultValue = "1")Integer page,String type) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		Pagination pagination = new Pagination();
		try {
	
			pagination.setPageSize(10);
			pagination.setPageNo(page);
			FinetTalkQo finettalkQo=new FinetTalkQo();
			finettalkQo.setSeachfinetTalkid(true);
			if(StringUtils.isNotEmpty(type)){
				finettalkQo.setNavigation(Integer.parseInt(type));
			}
			
			pagination.setCondition(finettalkQo);
			pagination = finettalkService.queryPagination(pagination);
			List<FinetTalk> list=(List<FinetTalk>)pagination.getList();
			SysProperties sysProperties=SysProperties.getInstance();
			String imagePath=sysProperties.get("imagePath");
			for(FinetTalk finetTalk:list) {
				if(StringUtils.isNotEmpty(finetTalk.getImge())){
					finetTalk.setImge(imagePath+"/finetTalk/"+finetTalk.getImge());
				}
			}
			resMap.put("code", "200");
			resMap.put("message", pagination);
			return JSONObject.fromObject(resMap).toString();

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "系统异常");
			return JSONObject.fromObject(resMap).toString();
		}


	}
	//精讲：精讲id查看
	@ResponseBody
	@RequestMapping("/speak_view")
	public String speakview(@RequestParam(value="speakid", required=true)String speakid) {
		Map<String, Object> resMap=new HashMap<String, Object>();
//		FinetTalkDto finetTalkDto=new FinetTalkDto();
		SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {

			FinetTalkQo finettalkQo=new FinetTalkQo();
			finettalkQo.setFinetTalkid(Long.parseLong(speakid));
			finettalkQo.setSeachfinetTalkid(true);
			FinetTalk finetTalk=finettalkService.queryUnique(finettalkQo);

			if(finetTalk.getReleasetime()!=null) {
				finetTalk.setReleasedate(format1.format(finetTalk.getReleasetime()));
			}
			if(finetTalk.getPageviews()!=null){
				finetTalk.setPageviews(finetTalk.getPageviews()+1);
			}else{
				finetTalk.setPageviews(1);
			}
			finettalkService.update(finetTalk);
			FinetTalkCommentQo qo=new FinetTalkCommentQo();
	
			qo.setFinetTalkId(Long.parseLong(speakid));
			Integer num=finetTalkCommentService.queryCount(qo);
			finetTalk.setCommentNum(num);
			resMap.put("code", "200");
			resMap.put("message",finetTalk);
			return JSONObject.fromObject(resMap).toString();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resMap.put("code", "200");
			resMap.put("message","出现异常！");
			return JSONObject.fromObject(resMap).toString();
		}


	}
	


}
